/**
 * 排序
 * 红：0
 * 白：1
 * 蓝：2
 */

/**
 * 方法1：两次确定
 * @param nums
 */
/*const sortColors = function(nums) {
  const swap = function (index1, index2) {
    let temp = nums[index1];
    nums[index1] = nums[index2];
    nums[index2] = temp;
  }
  let pre = 0;
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] === 0) {
      swap(pre, i);
      pre++;
    }
  }
  for (let i = pre; i < nums.length; i++) {
    if (nums[i] === 1) {
      swap(pre, i);
      pre++;
    }
  }
};*/

/**
 * 方法2：两个指针
 */
const sortColors = function (nums) {
  const swap = (index1, index2) => {
    let temp = nums[index1];
    nums[index1] = nums[index2];
    nums[index2] = temp;
  };
  let p0 = 0;
  let p1 = 0;
  for (let i = 0; i < nums.length; i++) {
    switch (nums[i]) {
      case 0:
        swap(p0, i);
        if (p0 < p1) {
          swap(p1, i);
        }
        p1++;
        p0++;
        break;
      case 1:
        swap(p1, i);
        p1++;
    }
  }
}